//
//  NLTabBarController.swift
//  sinaWeibo7110
//
//  Created by 安静清晨 on 2017/1/4.
//  Copyright © 2017年 walk-in-minds. All rights reserved.
//

import UIKit

class NLTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //加号的闭包实现
        
        let t = NLTabBar()
   
        
        //MARK: tabBar是只读属性, 不能直接赋值. 用KVC.
//        self.tabBar = NLTabBar()
        
        self.setValue(t, forKey: "tabBar")
        
        
        //此处形成了循环引用
        //MARK:flag
        t.btnClosure = { [unowned self] in
            
            //print(111)
            print(self)
            
            let moreView = NLComposeView(frame: UIScreen.main.bounds)
            
            UIApplication.shared.keyWindow?.addSubview(moreView)
            
            
        }
        
        createChildControllers()
        
    }

    //创建所有子控制器
    func createChildControllers(){
        
        createOneChildController(vc: NLHomeController(), title: "首页", imageName: "tabbar_home")
        
        
        createOneChildController(vc: NLMessageController(), title: "消息", imageName: "tabbar_message_center")
        
        createOneChildController(vc: NLDiscoverController(), title: "发现", imageName: "tabbar_discover")
        
        createOneChildController(vc: NLProfileController(), title: "我的", imageName: "tabbar_profile")
        
    }

    //创建单个子控制器
    var index : Int = 0
    func createOneChildController(vc:UIViewController, title:String, imageName:String){
        
        
//        vc.view.backgroundColor = UIColor.white
        
    
        //====1. 上下的title分开来写
//        vc.title = title
        vc.tabBarItem.title = title
        vc.navigationItem.title = "新浪" + title
        vc.tabBarItem.tag = index
        index += 1
        //====2. tabbar字体颜色
        //1)可以统一设置.简单粗暴.
//        self.tabBar.tintColor = UIColor.orange
        //2)专门设置某个vc的tabbar的title颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        
        //====3. renderMode
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //====4. 解决tabbar图标或文字位置不对的情况
//        vc.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
        
        
        //====5. 气泡
//        vc.tabBarItem.badgeValue = "24"
//        vc.tabBarItem.badgeColor = UIColor.brown
        
        let nc = NLBaseNavController(rootViewController: vc)
        
        addChildViewController(nc)
        
        
        
        //补充:
        //添加子视图控制器,使用这种方式在自定义tabbar的时候可能会出现tabbar点击不响应的bug
        //如果出现了该问题就可以直接通过setChildViewControllers: animated: 来解决
        
    }
    
    
    //点击tabBarButton的动画效果
    //利用tabbar的被点击的代理方法
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        //tag值在创建tabBar子控制器的时候设置好
        print(item.tag)
        var indexx : Int = 0
        //要拿到被点击的那个UITabBarItem.
        //先遍历tabBar的子视图拿到被点击的所有UITabBarItem再根据tag找出具体是哪一个UITabBarItem
        for subview in tabBar.subviews{
            
            if subview .isKind(of: NSClassFromString("UITabBarButton")!){
                
                    
                    if indexx == item.tag {
                        //得到的是一个小图➕文字. 
                        for xx in subview.subviews{
                            
                            if xx.isKind(of: NSClassFromString("UITabBarSwappableImageView")!){
                                // 找到小图
                                
                                xx.transform = CGAffineTransform(scaleX: 0.4, y: 0.4)
                                UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 5, options: [], animations: {
                                    xx.transform = CGAffineTransform.identity


                                }, completion: { (_) in

                                    print("ok")
                                })
                                
                     
                                
                            }
                            
                            
                        }
                        
                        
                        
                        
                        
                    }
                    
                    indexx += 1
                
                
            }
            
        
        }
        
        
        
    }

    
    
}
